import time
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer


class JwtUtil:
    secret_key = "sn30p4PhTf7XwODmiI4b4Xt2tONuqLLS"

    # 生成证书token
    @staticmethod
    def gen_cert_token(hash_data: str, expire_time: int):
        # 1. 到期时间戳
        exp = int(time.time()) + expire_time
        # 实例化Serializer
        s = Serializer(
            secret_key=JwtUtil.secret_key,
            algorithm_name='HS256',
            expires_in=expire_time,  # 设置过期时间
        )

        # 配置载荷
        payload = {
            'tokenIat': int(time.time()),  # token签发时间
            'exp': exp,  # 到期时间戳
            'id': hash_data,  # 设置id
        }
        # token加密
        token = s.dumps(
            payload
        )

        return token.decode('ascii'), payload
